using GameFramework.Event;
using HotUpdate.GameEntry;
using System;
using System.Reflection;
namespace HotUpdate
{
    public static class EventTool
    {
        #region 事件订阅
        /// <summary>
        /// 为instance订阅事件，处理函数为EventHandlerAttribute所标记的函数
        /// 只能是成员函数
        /// </summary>
        /// <param name="instance"></param>
        public static void SubscribeEventsTo<T>(T instance)
        {
            if (instance == null) { return; }
            var t = typeof(T);
            var ms = t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (var m in ms)
            {
                var attrs = m.GetCustomAttributes<EventHandlerAttribute>();                
                foreach (var attr in attrs)
                {
                    Delegate d = Delegate.CreateDelegate(typeof(EventHandler<GameEventArgs>), instance, m);
                    //避免重复订阅
                    if (!HotUpdateGameEntry.Event.Check(attr.EventID, d as EventHandler<GameEventArgs>))
                    {
                        HotUpdateGameEntry.Event.Subscribe(attr.EventID, d as EventHandler<GameEventArgs>);
                    }
                }

            }
        }

        /// <summary>
        /// 订阅事件
        /// </summary>
        /// <param name="instance"></param>
        public static void SubscribeEvent(int eventId, EventHandler<GameEventArgs> handler)
        {
            if (!HotUpdateGameEntry.Event.Check(eventId, handler))
            {
                HotUpdateGameEntry.Event.Subscribe(eventId, handler);
            }
        }

        /// <summary>
        /// 为instance取消订阅事件，处理函数为EventHandlerAttribute所标记的函数
        /// </summary>
        /// <param name="instance"></param>
        public static void UnSubscribeEventsTo<T>(T instance)
        {
            if (instance == null) { return; }
            var t = typeof(T);
            var ms = t.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (var m in ms)
            {
                var attrs = m.GetCustomAttributes<EventHandlerAttribute>();
                foreach (var attr in attrs)
                {
                    Delegate d = Delegate.CreateDelegate(typeof(EventHandler<GameEventArgs>), instance, m);
                    if (HotUpdateGameEntry.Event.Check(attr.EventID, d as EventHandler<GameEventArgs>))
                    {
                        HotUpdateGameEntry.Event.Unsubscribe(attr.EventID, d as EventHandler<GameEventArgs>);
                    }
                }
            }
        }

        /// <summary>
        /// 订阅事件
        /// </summary>
        /// <param name="instance"></param>
        public static void UnsubscribeEvent(int eventId, EventHandler<GameEventArgs> handler)
        {
            if (HotUpdateGameEntry.Event.Check(eventId, handler))
            {
                HotUpdateGameEntry.Event.Unsubscribe(eventId, handler);
            }
        }
        #endregion
    }
}

